home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk11 / rex / rexx.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  236 lines

  1.  
  2. /*
  3.  *  REXX.C
  4.  *
  5.  *      (c) Copyright 1987, 1988 by Kim DeVaughn, All Rights Reserved
  6.  *
  7.  *      This program is freely redistributable for non-commercial purposes.
  8.  *
  9.  *      For commercial use, you may contact the author via USENET at:
  10.  *          kim@amdahl.amdahl.com
  11.  *
  12.  *
  13.  *  ARexx interface code, etc. to interface with Matt Dillon's "dme" editor
  14.  *  (v1.29).
  15.  *
  16.  */
  17.  
  18.  
  19. #include "defs.h"
  20. #include "rexx.h"
  21.  
  22. #if AREXX
  23.  
  24. APTR OpenLibrary();
  25. APTR FindPort();
  26. APTR GetMsg();
  27. APTR CreateRexxMsg();
  28. APTR CreateArgstring();
  29.  
  30. extern int foundcmd;
  31.  
  32. struct RxsLib *RexxSysBase;
  33.  
  34.  
  35.  
  36. /*
  37.  * initialization for ARexx ... just open rexsyslib.library
  38.  */
  39.  
  40. void
  41. openrexx()
  42. {
  43.     RexxSysBase = (struct RxsLib *)OpenLibrary("rexxsyslib.library", (ULONG)RXSVERS);
  44.     return();
  45. }
  46.  
  47.  
  48.  
  49. /*
  50.  * cleanup any open ARexx stuff ...  just close rexsyslib.library for now
  51.  */
  52.  
  53. void
  54. closerexx()
  55. {
  56.     if (RexxSysBase) {
  57.         CloseLibrary(RexxSysBase);
  58.     }
  59.     return();
  60. }
  61.  
  62.  
  63.  
  64. /*
  65.  *  explicit invocation interface between do_command() and do_rexx
  66.  *  for ARexx macros having NO arguments (i.e., for the "rx" command)
  67.  */
  68.  
  69. do_rx()
  70. {
  71.     do_rexx(av[1]);
  72.     return();
  73. }
  74.  
  75.  
  76.  
  77. /*
  78.  *  explicit invocation interface between do_command() and do_rexx
  79.  *  for ARexx macros having ONE argument (i.e., for the "rx1" command)
  80.  */
  81.  
  82. do_rx1()
  83. {
  84.     char macbuf[256];
  85.  
  86.     strcpy(macbuf, av[1]);
  87.     strcat(macbuf, " ");
  88.     strcat(macbuf, av[2]);
  89.     do_rexx(macbuf);
  90.     return();
  91. }
  92.  
  93.  
  94.  
  95. /*
  96.  *  explicit invocation interface between do_command() and do_rexx
  97.  *  for ARexx macros having TWO arguments (i.e., for the "rx2" command)
  98.  */
  99.  
  100. do_rx2()
  101. {
  102.     char macbuf[256];
  103.  
  104.     strcpy(macbuf, av[1]);
  105.     strcat(macbuf, " ");
  106.     strcat(macbuf, av[2]);
  107.     strcat(macbuf, " ");
  108.     strcat(macbuf, av[3]);
  109.     do_rexx(macbuf);
  110.     return();
  111. }
  112.  
  113.  
  114.  
  115. /*
  116.  *  issue a command to ARexx ...
  117.  */
  118.  
  119. do_rexx(macstr)
  120. char *macstr;
  121. {
  122.     struct RexxArg *macarg;
  123.  
  124.     struct MsgPort  RexxPort;
  125.     struct MsgPort *ARexxPort;
  126.  
  127.     struct RexxMsg *macptr;
  128.     struct RexxMsg *cmdptr;
  129.  
  130.     char host[16];
  131.     char hexbuf[12];        /* should only need 9 bytes */
  132.     char errmsg[80];        /* don't build a larger error message */
  133.  
  134.     int  ret;
  135.  
  136.  
  137.     if (RexxSysBase == 0) {
  138.         title("Unknown Command   -   No Macros:  ARexx Not Installed ");   /* no rexxsyslib */
  139.         return(0);
  140.     }
  141.  
  142.  
  143.     ClearMem(&RexxPort, sizeof(struct MsgPort));
  144.     strcpy(host, "DME");
  145.     sprintf(hexbuf, "%08x", &RexxPort);
  146.     strcat(host, hexbuf);
  147.     InitPort(&RexxPort, host);      /* need to error check this */
  148.     AddPort(&RexxPort);
  149.     /* return here if InitPort failed */
  150.  
  151.  
  152.     if (macarg = (struct RexxArg *)CreateArgstring(macstr, strlen(macstr))) {
  153.         if (macptr = (struct RexxMsg *)CreateRexxMsg(&RexxPort, "dme", host)) {
  154.             ACTION(macptr) = RXCOMM;
  155.             ARG0(macptr)   = (STRPTR)macarg;
  156.  
  157.             Forbid();
  158.             if (ARexxPort = (struct MsgPort *)FindPort("REXX")) {
  159.                 PutMsg(ARexxPort, macptr);
  160.                 Permit();
  161.                 title("Calling ARexx Macro ... ");
  162.  
  163.                 for (;;) {
  164.                     WaitPort(&RexxPort);
  165.                     cmdptr = (struct RexxMsg *)GetMsg(&RexxPort);
  166.  
  167.                     if (IsRexxMsg(cmdptr)) {
  168.  
  169.                         foundcmd = 0;
  170.                         ret = do_command(ARG0(cmdptr));
  171.                         if (foundcmd) {
  172.                             ret = (ret == 1) ? 0 : 5;   /* cmd error:  RC = 5  */
  173.                         } else {
  174.                             ret = do_rexx(ARG0(cmdptr));    /* another macro? */
  175.                         }
  176.  
  177.                         RESULT1(cmdptr) = ret;
  178.                         RESULT2(cmdptr) = 0;
  179.                         ReplyMsg(cmdptr);
  180.                     }
  181.                     do_command("null");     /* a kludge to set "foundcmd" */
  182.                     if (macptr == cmdptr) break;
  183.                 }
  184.  
  185.  
  186.                 if (ret = RESULT1(cmdptr)) {
  187.                     if (RESULT2(cmdptr)) {
  188.                         if (RESULT2(cmdptr) == 1) {
  189.                             title("Unknown Command ");
  190.                         } else {
  191.                             sprintf(errmsg, "ARexx Macro Error:  Code = %d  Severity = %d ", RESULT2(cmdptr), ret);
  192.                             title(errmsg);
  193.                         }
  194.                     } else {
  195.                         sprintf(errmsg, "User Specified Macro Error:  RC = %d ", ret);
  196.                         title(errmsg);
  197.                     }
  198.                 } else {
  199.                     title("OK ");
  200.                 }
  201.             } else {
  202.                 title("Unknown Command   -   No Macros:  ARexx Not Active ");   /* no REXX port */
  203.                 ret = -1;
  204.             }
  205.             DeleteRexxMsg(macptr);
  206.         } else {
  207.             title("CreateRexxMsg() Failed ");   /* may be overkill, and not need to ckeck this */
  208.             ret = -1;
  209.         }
  210.         DeleteArgstring(macarg);
  211.     } else {
  212.         title("CreateArgstring() Failed ");     /* may be overkill, and not need to check this */
  213.         ret = -1;
  214.     }
  215.  
  216.  
  217.     RemPort(&RexxPort);
  218.     FreePort(&RexxPort);
  219.     return(ret);
  220. }
  221.  
  222.  
  223.  
  224. /*
  225.  *  a kludge to flush the do_command pipe ... until I can fix a recursive macro bug ...
  226.  */
  227.  
  228. void
  229. do_null()
  230. {
  231.     return();
  232. }
  233.  
  234. #endif
  235.  
  236.